package com.szsky.sky.demo;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;

public class ASEUtil_TYYZ {

    private static final Logger logger = LoggerFactory.getLogger(ASEUtil_TYYZ.class);


    private static final String KEY = "NQlrO$8*ziPls@w0";


    private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";


    public static String aesDecrypt(String encrypt) {
        try {
            return aesDecrypt(encrypt, "NQlrO$8*ziPls@w0");
        } catch (Exception e) {
            logger.error("aes解密失败", e);
            return "";
        }
    }


    public static String aesEncrypt(String content) {
        try {
            return aesEncrypt(content, "NQlrO$8*ziPls@w0");
        } catch (Exception e) {
            logger.error("aes加密失败", e);
            return "";
        }
    }


    public static String binary(byte[] bytes, int radix) {
        return (new BigInteger(1, bytes)).toString(radix);
    }


    public static String base64Encode(byte[] bytes) {
        return Base64.encodeBase64String(bytes);
    }

    public static byte[] base64Decode(String base64Code) throws Exception {
        return StringUtils.isEmpty(base64Code) ? null : Base64.decodeBase64(base64Code);
    }


    public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(1, new SecretKeySpec(encryptKey.getBytes(), "AES"));

        return cipher.doFinal(content.getBytes("utf-8"));
    }

    public static String aesEncrypt(String content, String encryptKey) throws Exception {
        return base64Encode(aesEncryptToBytes(content, encryptKey));
    }


    public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);

        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(2, new SecretKeySpec(decryptKey.getBytes(), "AES"));
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        return new String(decryptBytes);
    }

    public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
        return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
    }

    public static void main(String[] args) throws Exception {
        /*
        d145fd2a25d05c21bedf2beffccf9d6db242dbf8c893acdae7f21b3cb4b795e7
        92434f3fd1f694baa5f52449a52cbdae04dbfc388a47fd8d4b8528283cfaadaf
        94be5921c87b1c07fd04b0e57ec5fe79c2c2c53e255bf832de2cb770c73ebf00
        bddd880bd3cd5264d5a8ace2c9ee0581c50968b947fea44b26b572cd077fbd98
        */

//        System.out.println(aesDecryptByBytes("d145fd2a25d05c21bedf2beffccf9d6db242dbf8c893acdae7f21b3cb4b795e7".getBytes(StandardCharsets.UTF_8),"NQlrO$8*ziPls@w0"));
        System.out.println(aesDecrypt("RoBafNNRDf4rXg5+fv0QiA=="));
        System.out.println(aesDecrypt("RoBafNNRDf4rXg5+fv0QiA==","NQlrO$8*ziPls@w0"));
    }
}
